﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.EventAggregators;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.UserManagement.Models;

namespace IntoNails.Modules.UserManagement.ViewModels
{
    public partial class UserWorkspaceViewModel
    {
        private void OnCreateNewUserEventSubscribed(User user)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            UserDataContext dataContext = new UserDataContext(user.Authority, user.Gender, false);

            this.CreateNewUserNotification.Raise(
                user,
                dataContext,
                this.CreateNewUserCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true)
                );
        }

        private void CreateNewUserCallback(User userCallback)
        {
            // TODO: check input
            this._userRepository.Add(userCallback);
            this._userRepository.Save();

            // add new user to list
            this.UserCollection.Add(userCallback);

            // unlock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }
        
        private void OnEditUserEventSubscribed(object parameter)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedUser == null)
            {
                MessageBoxContext context = new MessageBoxContext("Please select a Employee to edit first.",
                                                                  GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                                                                  MessageBoxIcons.Exclamation);
                this.ShowMessageBox.Raise(context,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            UserDataContext dataContext = new UserDataContext(this.SelectedUser.Authority, this.SelectedUser.Gender, true);

            this.CreateNewUserNotification.Raise(
                this.SelectedUser,
                dataContext,
                this.EditUserCallback,
                () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void EditUserCallback(User userCallback)
        {
            if (userCallback == null)
            {
                throw new InvalidOperationException("userCallback");
            }

            userCallback.UpdatedDate = DateTime.Now;

            ////int index = this.UserCollection.IndexOf(userCallback);
            ////this.UserCollection[index] = userCallback;

            //// //set the selected user to the callback user.
            ////this.SelectedUser = userCallback;

            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);

            ////this.PopulateUserList();

            // TODO: check input
            ////this._userRepository.Update(userCallback.UniqueCode, userCallback);
            ////this._userRepository.Add(userCallback);
            this._userRepository.Save();
        }

        private void OnDeleteUserEventSubscribed(MessageBoxContext context)
        {
            // lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(false);

            if (this.SelectedUser == null)
            {
                MessageBoxContext warningMsgBoxContext = new MessageBoxContext(
                    "Please select a Employee to edit first.",
                    GlobalInfoCaches.AssemblyName, MessageBoxButtons.OK,
                    MessageBoxIcons.Exclamation);

                this.ShowMessageBox.Raise(warningMsgBoxContext,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            if (this.SelectedUser.Authority == UserType.Admin)
            {
                MessageBoxContext warningMsgBoxContext = new MessageBoxContext("Cannot delete Admin.",
                                                                               GlobalInfoCaches.AssemblyName,
                                                                               MessageBoxButtons.OK,
                                                                               MessageBoxIcons.Exclamation);

                this.ShowMessageBox.Raise(warningMsgBoxContext,
                                          boxContext => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true),
                                          () => this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));

                return;
            }

            this.ShowMessageBox.Raise(context, this.MessageBoxSubmitCallback,
                                      () =>
                                      this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true));
        }

        private void MessageBoxSubmitCallback(MessageBoxContext confirmedDeleteMsgBoxCallback)
        {
            // update data-storage
            this._userRepository.Remove(this.SelectedUser.UniqueCode);
            this._userRepository.Save();

            // update UI
            this.UserCollection.Remove(this.SelectedUser);

            // un-lock the ribbon-bar
            this._eventAggregator.GetEvent<UnLockRibbonBarEventAggregators>().Publish(true);
        }
    }
}
